package com.southpark.framework.control.responders
{	
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.southpark.framework.control.commands.CommandFault;
	import com.southpark.framework.control.events.IEvent;
	
	
	public class AbstractResponder implements IResponder,ICommand
	{
		
		private var responder:IResponder;
		
		public function AbstractResponder(responder:IResponder=null)
		{
			this.responder = responder;
		}
		
		
		/**
		 * ICommand
		 */
		public function execute(cairngormEvent:CairngormEvent):void
		{
			var event:IEvent = cairngormEvent as IEvent;
			responder = event.responder;
		}
		
		
		/**
		 * IResponder
		 */
		public function onSuccess(result:Object):void
		{
			if (responder)
			{
				responder.onSuccess(result);
				responder = null;
			};
		}
		
		public function onFault(commandFault:CommandFault):Boolean
		{
			if (commandFault)
			{
				if (responder)
				{
					if (responder.onFault(commandFault) == false)
					{
						onError(commandFault);
					};
				}
				else
				{
					onError(commandFault);
				};
			}
			else
			{
				if (responder)
				{
					responder.onFault(null);
				};
			};
			
			return true;
		}
		
		
		/**
		 * 
		 */
		protected function onError(commandFault:CommandFault):void
	    {
	    	trace("proxyClassName: "+commandFault.proxyClassName);
	    	trace("backendMethod: "+commandFault.backendMethod);
	    	trace("fault: "+commandFault.fault);
	    }
	    

	}
}